﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using TMPro.EditorUtilities;

public class LoginPanel : BasePanel
{
    public Button confirmBtn;
    public Button RegisterBtn;

    public TMP_InputField userField;
    public TMP_InputField pwField;

    public Toggle rememberToggle;
    public Toggle autoToggle;

    protected override void Init()
    {
        confirmBtn.onClick.AddListener(() =>
        {
            if (userField.text.Length <= 6 || pwField.text.Length <= 6)
            {
                pwField.text = "";
                UIManager.Instance.ShowPanel<TipPanel>().ChangeInfo("用户名和密码必须大于6位");
                return;
            }

            //检测账号和密码是否匹配
            if (LoginManager.Instance.VerifyInfo(userField.text, pwField.text))
            {
                //保存当前信息
                LoginManager.Instance.LoginData.userName = userField.text;
                LoginManager.Instance.LoginData.password = pwField.text;
                LoginManager.Instance.LoginData.rememberOn = rememberToggle.isOn;
                LoginManager.Instance.LoginData.autoOn = autoToggle.isOn;
                

                LoginManager.Instance.SaveLoginData();

                if (LoginManager.Instance.LoginData.preServerID < 0)
                {
                    UIManager.Instance.ShowPanel<ChoseServerPanel>();
                }
                else
                { 
                    UIManager.Instance.ShowPanel<ServerPanel>();              
                }


                UIManager.Instance.HidePanel<LoginPanel>();
                return;
            }
            else
            {
                pwField.text = "";
                UIManager.Instance.ShowPanel<TipPanel>().ChangeInfo("用户不存在或者密码错误");
                return;
            }

        });

        RegisterBtn.onClick.AddListener(() => 
        {
            //隐藏当前Panel，进入注册Panel
            UIManager.Instance.HidePanel<LoginPanel>();
            UIManager.Instance.ShowPanel<RegisterPanel>();
        }); 


        rememberToggle.onValueChanged.AddListener((isON) =>
        {
            //如果此时不记住密码，那么自动登录也要false
            if (!isON)
            {
                autoToggle.isOn = isON;
            }
        });

        autoToggle.onValueChanged.AddListener((isON) =>
        {
            if (isON)
            {
                rememberToggle.isOn = isON;
            }
        });

    }


    public override void ShowMe()
    {
        base.ShowMe();

        LoginData loginData = LoginManager.Instance.LoginData;

        rememberToggle.isOn = loginData.rememberOn;
        autoToggle.isOn = loginData.autoOn; 

        userField.text = loginData.userName;
        if (rememberToggle.isOn)
        { 
            pwField.text = loginData.password;
        }

        if (autoToggle.isOn)
        {
            if (LoginManager.Instance.VerifyInfo(loginData.userName, loginData.password))
            {
                if (loginData.preServerID < 0)
                {
                    UIManager.Instance.ShowPanel<ChoseServerPanel>();
                }
                else
                {
                    UIManager.Instance.ShowPanel<ServerPanel>();
                }

                UIManager.Instance.HidePanel<LoginPanel>(false);
            }
            else
            {
                UIManager.Instance.ShowPanel<TipPanel>().ChangeInfo("密码错误");
            }
        }

    }

    public void ChangeUserAndPW(string user, string password)
    {
        userField.text = user;
        pwField.text = password;
    }
}
